| Conditions | 12 |
| Paths | 44 |
| Total Lines | 62 |
| Lines | 35 |
| Ratio | 56.45 % |
| Changes | 11 | ||
| Bugs | 3 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like svg.js ➔ prepareSVG often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import merge from "../help/merge.js"; |
||
| 26 | this.drawSVGText(group, encodingOptions, encoding); |
||
| 27 | |||
| 28 | currentX += encoding.width; |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | prepareSVG(){ |
||
| 33 | // Clear the SVG |
||
| 34 | while (this.svg.firstChild) { |
||
| 35 | this.svg.removeChild(this.firstChild); |
||
| 36 | } |
||
| 37 | |||
| 38 | calculateEncodingAttributes(this.encodings, this.options); |
||
| 39 | var totalWidth = getTotalWidthOfEncodings(this.encodings); |
||
| 40 | var maxHeight = getMaximumHeightOfEncodings(this.encodings); |
||
| 41 | |||
| 42 | var width = totalWidth + this.options.marginLeft + this.options.marginRight; |
||
| 43 | this.setSvgAttributes(width, maxHeight); |
||
| 44 | } |
||
| 45 | |||
| 46 | drawSvgBarcode(parent, options, encoding){ |
||
| 47 | var binary = encoding.data; |
||
| 48 | |||
| 49 | // Creates the barcode out of the encoded binary |
||
| 50 | var yFrom; |
||
| 51 | if(options.textPosition == "top"){ |
||
| 52 | yFrom = options.fontSize + options.textMargin; |
||
| 53 | } |
||
| 54 | else{ |
||
| 55 | yFrom = 0; |
||
| 56 | } |
||
| 57 | |||
| 58 | var barWidth = 0; |
||
| 59 | var x = 0; |
||
| 60 | for(var b = 0; b < binary.length; b++){ |
||
| 61 | x = b * options.width + encoding.barcodePadding; |
||
| 62 | |||
| 63 | if(binary[b] === "1"){ |
||
| 64 | barWidth++; |
||
| 65 | } |
||
| 66 | else if(barWidth > 0){ |
||
| 67 | drawLine(x - options.width * barWidth, yFrom, options.width * barWidth, options.height, parent); |
||
| 68 | barWidth = 0; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | // Last draw is needed since the barcode ends with 1 |
||
| 73 | if(barWidth > 0){ |
||
| 74 | drawLine(x - options.width * (barWidth - 1), yFrom, options.width * barWidth, options.height, parent); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | drawSVGText(parent, options, encoding){ |
||
| 79 | var textElem = document.createElementNS(svgns, 'text'); |
||
| 80 | |||
| 81 | // Draw the text if displayValue is set |
||
| 82 | if(options.displayValue){ |
||
| 83 | var x, y; |
||
| 84 | |||
| 85 | textElem.setAttribute("style", |
||
| 86 | "font:" + options.fontOptions + " " + options.fontSize + "px " + options.font |
||
| 87 | ); |
||
| 88 | |||
| 170 |